<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
    const TABLE_PREFIX = '$_';
    const FIELD_PREFIX = '$_';
    public $map = NULL;

    public function __construct()
    {
        parent::__construct();
        if ($this->map === NULL) {
            $this->map = new TABLE_CLASS();
        }
    }
}

class MY_AdminController extends MY_Controller
{
    const VIEW_FOLDER = 'admin/';
    const MODEL_FOLDER = 'admin/';
    const LANG_FOLDER = '/admin';
    const CSS_FOLDER = 'assets/skins/admin/css/';
    const JAVASCRIPT_FOLDER = 'assets/skins/admin/js/';
    const USER_INFO = 'userInfo';
    public static $_Lang = 'en';
    public static $_TokenName;
    public static $_TokenValue;


    public function __construct()
    {
        parent::__construct();
        $this->_LangLoad(MY_AdminController::$_Lang);
        MY_AdminController::$_TokenName = $this->security->get_csrf_token_name();
        MY_AdminController::$_TokenValue = $this->security->get_csrf_hash();
    }

    function _LangLoad($lang)
    {
        $this->lang->load('error', $lang . MY_AdminController::LANG_FOLDER);
        $this->lang->load('form', $lang . MY_AdminController::LANG_FOLDER);
        $this->lang->load('menu', $lang . MY_AdminController::LANG_FOLDER);
        $this->lang->load('message', $lang . MY_AdminController::LANG_FOLDER);
        $this->lang->load('text', $lang . MY_AdminController::LANG_FOLDER);
    }
}

/*-------------------------------------------------************-------------------------------------------------------*/
/*-------------------------------------------------MAP DATABASE-------------------------------------------------------*/
/*-------------------------------------------------************-------------------------------------------------------*/

class BASE_MAP_CLASS
{
    public static function SetPrefix($obj, $prefix)
    {
        $class = new ReflectionClass(get_class($obj));
        foreach ($class->getProperties() as $property) {
            $class->getProperty($property->name)->setValue($obj, $prefix . $property->name);
        }
    }
}

class TABLE_CLASS
{
    public $power;
    public $user;
    public $type_input;
    public $type_output;
    public $field;
    public $table;

    public function __construct()
    {
        BASE_MAP_CLASS::SetPrefix($this, MY_Controller::TABLE_PREFIX);
    }

    public static function power()
    {
        return new TABLE_POWER_CLASS();
    }

    public static function user()
    {
        return new TABLE_USER_CLASS();
    }

    public static function type_input()
    {
        return new TABLE_TYPE_INPUT_CLASS();
    }

    public static function type_output()
    {
        return new TABLE_TYPE_OUTPUT_CLASS();
    }

    public static function field()
    {
        return new TABLE_FIELD_CLASS();
    }

    public static function table()
    {
        return new TABLE_TABLE_CLASS();
    }
}

class TABLE_POWER_CLASS
{
    public $ID;
    public $Name;
    public $Description;
    public $Created;
    public $UserID;
    public $Activated;
    public $Deleted;

    public function __construct()
    {
        BASE_MAP_CLASS::SetPrefix($this, MY_Controller::FIELD_PREFIX);
    }

    public function field()
    {
        return array(
            $this->ID => array(
                'type' => 'INT',
                'constraint' => '10',
                'unsigned' => TRUE,
                'auto_increment' => TRUE
            ),
            $this->Name => array(
                'type' => 'VARCHAR',
                'constraint' => '255'
            ),
            $this->Description => array(
                'type' => 'TEXT',
                'default' => ''
            ),
            $this->Created => array(
                'type' => 'INT',
                'constraint' => '11',
                'default' => '0'
            ),
            $this->UserID => array(
                'type' => 'INT',
                'constraint' => '10',
                'unsigned' => TRUE,
            ),
            $this->Activated => array(
                'type' => 'TINYINT',
                'constraint' => '1',
                'default' => '0'
            ),
            $this->Deleted => array(
                'type' => 'TINYINT',
                'constraint' => '1',
                'default' => '0'
            ),
        );
    }

    public function primaryKey()
    {
        return array(
            $this->ID
        );
    }

    public function key()
    {
        return array(
            $this->Name,
            $this->Created,
            $this->UserID,
            $this->Activated,
            $this->Deleted
        );
    }
}

class TABLE_USER_CLASS
{
    public $ID;
    public $Username;
    public $Password;
    public $Email;
    public $PowerID;
    public $Key;
    public $Created;
    public $UserID;
    public $Activated;
    public $Deleted;

    public function __construct()
    {
        BASE_MAP_CLASS::SetPrefix($this, MY_Controller::FIELD_PREFIX);
    }

    public function field()
    {
        return array(
            $this->ID => array(
                'type' => 'INT',
                'constraint' => '10',
                'unsigned' => TRUE,
                'auto_increment' => TRUE
            ),
            $this->Username => array(
                'type' => 'VARCHAR',
                'constraint' => '100'
            ),
            $this->Password => array(
                'type' => 'VARCHAR',
                'constraint' => '40'
            ),
            $this->Email => array(
                'type' => 'VARCHAR',
                'constraint' => '255'
            ),
            $this->PowerID => array(
                'type' => 'INT',
                'constraint' => '10',
                'unsigned' => TRUE,
            ),
            $this->Key => array(
                'type' => 'VARCHAR',
                'constraint' => '50',
                'default' => ''
            ),
            $this->Created => array(
                'type' => 'INT',
                'constraint' => '11',
                'default' => '0'
            ),
            $this->UserID => array(
                'type' => 'INT',
                'constraint' => '10',
                'unsigned' => TRUE,
            ),
            $this->Activated => array(
                'type' => 'TINYINT',
                'constraint' => '1',
                'default' => '0'
            ),
            $this->Deleted => array(
                'type' => 'TINYINT',
                'constraint' => '1',
                'default' => '0'
            ),
        );
    }

    public function primaryKey()
    {
        return array(
            $this->ID
        );
    }

    public function key()
    {
        return array(
            $this->Username,
            $this->Email,
            $this->PowerID,
            $this->Created,
            $this->UserID,
            $this->Activated,
            $this->Deleted,
        );
    }
}

class TABLE_TYPE_INPUT_CLASS
{
    public $ID;
    public $Name;
    public $Description;
    public $Created;
    public $UserID;
    public $Activated;
    public $Deleted;

    public function __construct()
    {
        BASE_MAP_CLASS::SetPrefix($this, MY_Controller::FIELD_PREFIX);
    }

    public function field()
    {
        return array(
            $this->ID => array(
                'type' => 'INT',
                'constraint' => '10',
                'unsigned' => TRUE,
                'auto_increment' => TRUE
            ),
            $this->Name => array(
                'type' => 'VARCHAR',
                'constraint' => '255'
            ),
            $this->Description => array(
                'type' => 'TEXT',
                'default' => ''
            ),
            $this->Created => array(
                'type' => 'INT',
                'constraint' => '11',
                'default' => '0'
            ),
            $this->UserID => array(
                'type' => 'INT',
                'constraint' => '10',
                'unsigned' => TRUE,
            ),
            $this->Activated => array(
                'type' => 'TINYINT',
                'constraint' => '1',
                'default' => '0'
            ),
            $this->Deleted => array(
                'type' => 'TINYINT',
                'constraint' => '1',
                'default' => '0'
            ),
        );
    }

    public function primaryKey()
    {
        return array(
            $this->ID
        );
    }

    public function key()
    {
        return array(
            $this->Name,
            $this->Created,
            $this->UserID,
            $this->Activated,
            $this->Deleted
        );
    }
}

class TABLE_TYPE_OUTPUT_CLASS
{
    public $ID;
    public $Name;
    public $Description;
    public $Created;
    public $UserID;
    public $Activated;
    public $Deleted;

    public function __construct()
    {
        BASE_MAP_CLASS::SetPrefix($this, MY_Controller::FIELD_PREFIX);
    }

    public function field()
    {
        return array(
            $this->ID => array(
                'type' => 'INT',
                'constraint' => '10',
                'unsigned' => TRUE,
                'auto_increment' => TRUE
            ),
            $this->Name => array(
                'type' => 'VARCHAR',
                'constraint' => '255'
            ),
            $this->Description => array(
                'type' => 'TEXT',
                'default' => ''
            ),
            $this->Created => array(
                'type' => 'INT',
                'constraint' => '11',
                'default' => '0'
            ),
            $this->UserID => array(
                'type' => 'INT',
                'constraint' => '10',
                'unsigned' => TRUE,
            ),
            $this->Activated => array(
                'type' => 'TINYINT',
                'constraint' => '1',
                'default' => '0'
            ),
            $this->Deleted => array(
                'type' => 'TINYINT',
                'constraint' => '1',
                'default' => '0'
            ),
        );
    }

    public function primaryKey()
    {
        return array(
            $this->ID
        );
    }

    public function key()
    {
        return array(
            $this->Name,
            $this->Created,
            $this->UserID,
            $this->Activated,
            $this->Deleted
        );
    }
}

class TABLE_FIELD_CLASS
{
    public $ID;
    public $Name;
    public $DisplayName;
    public $Description;
    public $TableID;
    public $Type;
    public $Constraint;
    public $Default;
    public $Unsigned;
    public $Null;
    public $Auto_increment;
    public $TypeInputID;
    public $TypeOutputID;
    public $Order;
    public $Created;
    public $UserID;
    public $Activated;
    public $Deleted;

    public function __construct()
    {
        BASE_MAP_CLASS::SetPrefix($this, MY_Controller::FIELD_PREFIX);
    }

    public function field()
    {
        return array(
            $this->ID => array(
                'type' => 'INT',
                'constraint' => '10',
                'unsigned' => TRUE,
                'auto_increment' => TRUE
            ),
            $this->Name => array(
                'type' => 'VARCHAR',
                'constraint' => '255'
            ),
            $this->DisplayName => array(
                'type' => 'VARCHAR',
                'constraint' => '255'
            ),
            $this->Description => array(
                'type' => 'TEXT',
                'default' => ''
            ),
            $this->TableID => array(
                'type' => 'INT',
                'constraint' => '10',
                'unsigned' => TRUE,
            ),
            $this->Type => array(
                'type' => 'VARCHAR',
                'constraint' => '20'
            ),
            $this->Constraint => array(
                'type' => 'INT',
                'constraint' => '10',
                'unsigned' => TRUE,
                'null' => TRUE
            ),
            $this->Default => array(
                'type' => 'VARCHAR',
                'constraint' => '255',
                'null' => TRUE
            ),
            $this->Unsigned => array(
                'type' => 'TINYINT',
                'constraint' => '1',
                'default' => '0'
            ),
            $this->Null => array(
                'type' => 'TINYINT',
                'constraint' => '1',
                'default' => '0'
            ),
            $this->Auto_increment => array(
                'type' => 'TINYINT',
                'constraint' => '1',
                'default' => '0'
            ),
            $this->TypeInputID => array(
                'type' => 'INT',
                'constraint' => '10',
                'unsigned' => TRUE,
            ),
            $this->TypeOutputID => array(
                'type' => 'INT',
                'constraint' => '10',
                'unsigned' => TRUE,
            ),
            $this->Order => array(
                'type' => 'INT',
                'constraint' => '10',
                'unsigned' => TRUE,
            ),
            $this->Created => array(
                'type' => 'INT',
                'constraint' => '11',
                'default' => '0'
            ),
            $this->UserID => array(
                'type' => 'INT',
                'constraint' => '10',
                'unsigned' => TRUE,
            ),
            $this->Activated => array(
                'type' => 'TINYINT',
                'constraint' => '1',
                'default' => '0'
            ),
            $this->Deleted => array(
                'type' => 'TINYINT',
                'constraint' => '1',
                'default' => '0'
            ),
        );
    }

    public function primaryKey()
    {
        return array(
            $this->ID,
        );
    }

    public function key()
    {
        return array(
            $this->Name,
            $this->TableID,
            $this->TypeInputID,
            $this->TypeOutputID,
            $this->Order,
            $this->Created,
            $this->UserID,
            $this->Activated,
            $this->Deleted
        );
    }
}

class TABLE_TABLE_CLASS
{
    public $ID;
    public $Name;
    public $DisplayName;
    public $Description;
    public $Order;
    public $Created;
    public $UserID;
    public $Activated;
    public $Deleted;

    public function __construct()
    {
        BASE_MAP_CLASS::SetPrefix($this, MY_Controller::FIELD_PREFIX);
    }

    public function field()
    {
        return array(
            $this->ID => array(
                'type' => 'INT',
                'constraint' => '10',
                'unsigned' => TRUE,
                'auto_increment' => TRUE
            ),
            $this->Name => array(
                'type' => 'VARCHAR',
                'constraint' => '255'
            ),
            $this->DisplayName => array(
                'type' => 'VARCHAR',
                'constraint' => '255'
            ),
            $this->Description => array(
                'type' => 'TEXT',
                'default' => ''
            ),
            $this->Order => array(
                'type' => 'INT',
                'constraint' => '10',
                'unsigned' => TRUE,
            ),
            $this->Created => array(
                'type' => 'INT',
                'constraint' => '11',
                'default' => '0'
            ),
            $this->UserID => array(
                'type' => 'INT',
                'constraint' => '10',
                'unsigned' => TRUE,
            ),
            $this->Activated => array(
                'type' => 'TINYINT',
                'constraint' => '1',
                'default' => '0'
            ),
            $this->Deleted => array(
                'type' => 'TINYINT',
                'constraint' => '1',
                'default' => '0'
            ),
        );
    }

    public function primaryKey()
    {
        return array(
            $this->ID
        );
    }

    public function key()
    {
        return array(
            $this->Name,
            $this->Order,
            $this->Created,
            $this->UserID,
            $this->Activated,
            $this->Deleted
        );
    }
}